/**
 * 
 */
package com.huarui.desktop.service.impl;

import java.sql.Types;

import org.apache.commons.lang.ArrayUtils;
import com.huarui.desktop.model.TableColumnInfo;
import com.huarui.desktop.service.TableColumnResolver;

/**
 * @author sunpeng
 * 
 */
public class DefaultTableColumnResolver implements TableColumnResolver {
	/**
	 * 
	 * 
	 * System.out.println("=="+Types.INTEGER);
	 * System.out.println("=="+Types.VARCHAR);
	 * System.out.println("=="+Types.CHAR);
	 * System.out.println("=="+Types.DOUBLE);
	 * System.out.println("=="+Types.DECIMAL);
	 * System.out.println("=="+Types.DATE); System.out.println("=="+Types.TIME);
	 * System.out.println("=="+Types.TIMESTAMP);
	 * 
	 * @author sunpeng
	 * 
	 */
	enum RecordTyper {
		Common(new int[] { Types.VARCHAR, Types.CHAR, Types.INTEGER }) {
			public String getRecordType(TableColumnInfo column) {
				return "{name: '" + column.getName() + "'}";
			}
		},
		Datetime(new int[] { Types.DATE, Types.TIME, Types.TIMESTAMP }) {
			public String getRecordType(TableColumnInfo column) {
				return "{name: '" + column.getName() + "', type: 'date', dateFormat: 'Y-m-d'}";
			}
		},
		Double(new int[] { Types.DOUBLE, Types.DECIMAL }) {
			public String getRecordType(TableColumnInfo column) {
				return "{name: '" + column.getName() + "'}";
			}
		},
		Boolean(new int[] { Types.BIT, Types.BINARY }) {
			public String getRecordType(TableColumnInfo column) {
				return "{name: '" + column.getName() + "'}";
			}
		};
		private int[] types;

		RecordTyper(int[] types) {
			this.types = types;
		}

		public abstract String getRecordType(TableColumnInfo column);
	}

	enum RecordModer {
		Common(new int[] { Types.VARCHAR, Types.CHAR, Types.INTEGER }) {

		},
		Datetime(new int[] { Types.DATE, Types.TIME, Types.TIMESTAMP }) {
			public String getRecordMode(TableColumnInfo column) {
				return "{renderer: Ext.util.Format.dateRenderer('Y-m-d'), header: '" + column.getName() + "',width: " + column.getDisplaySize() + ",sortable: true, dataIndex: '" + column.getName()
						+ "'}";

			}
		};

		private int[] types;
		RecordModer(int[] types) {
			this.types = types;
		}

		public String getRecordMode(TableColumnInfo column) {
			return "{header:'" + column.getName() + "',width:" + column.getDisplaySize() + ",sortable:true,dataIndex:'" + column.getName() + "'}";
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.huarui.desktop.service.TableColumnResolver#getRecordType(com.huarui
	 * .desktop.model.TableColumnInfo)
	 */
	public String getRecordType(TableColumnInfo column) {
		int type = column.getType();
		for (RecordTyper typer : RecordTyper.values()) {
			if (ArrayUtils.contains(typer.types, type)) {
				return typer.getRecordType(column);
			}
		}
		return RecordTyper.Common.getRecordType(column);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.huarui.desktop.service.TableColumnResolver#getRecordMode(com.huarui
	 * .desktop.model.TableColumnInfo)
	 */
	public String getRecordMode(TableColumnInfo column) {
		int type = column.getType();
		for (RecordModer moder : RecordModer.values()) {
			if (ArrayUtils.contains(moder.types, type)) {
				return moder.getRecordMode(column);
			}
		}
		return RecordModer.Common.getRecordMode(column);
	}

}
